package com.pj.wisdomlogistics.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.pj.wisdomlogistics.MyApplication;


public class NetworkBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "NetworkReceiver";
    public static final int HAVE_NO_NETWORK = -1;

    public static int networkConnectType(Context context)
    {
        int type;

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (null != info && info.isConnectedOrConnecting())
        {
            Log.e(TAG, "网络类型为：" + info.getTypeName());
            Log.e(TAG, "网络类型为：" + info.getSubtypeName());
            type = info.getType();
        } else
        {
            //无网络
            type = HAVE_NO_NETWORK;
        }
        return type;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("test","action:"+action);
        if (!TextUtils.isEmpty(action)){
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){
                int type = networkConnectType(context);
                Log.e(TAG,"网络状态发生变化，状态：" + type);
                if(type == 1 || type == 0){
                    MyApplication.isNetOk = true;
                }else  if(type == -1){
                    MyApplication.isNetOk = false;
                }
               /* NetWorkChangeHelper.getIns().setChanged();
                NetWorkChangeHelper.getIns().notifyObservers(type);*/
            }
        }
    }
}
